import Axios from 'axios'
import qs from 'qs'
import {LOGIN_URL, REQ_IP} from "@/assets/common/js/config";
import {ss_getItem} from "@/assets/common/js/utils";

Axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
Axios.defaults.withCredentials = true;
Axios.defaults.baseURL = REQ_IP;

//请求开始
Axios.interceptors.request.use(config => {
  //判断请求是否包含不显示加载动画字段
  config.data ? config.data : config.data = {};
  if (config.data.loading === undefined || config.data.loading)

    //如果DATA中包含加载动画,从DATA中删除加载动画字段
    delete config.data.loading;

  //判断DATA原型是否为formData类型，在序列化数据
  if (!(config.data instanceof FormData)) {
    config.data ? config.data : config.data = {};
    if (config.url !== LOGIN_URL) {
      config.data.token = ss_getItem("token");
    }
    if (config.method === 'post')
      config.data = qs.stringify(config.data);
  } else {

  }
  return config;
}, error => {
  return Promise.reject(error);
});

//请求结束
Axios.interceptors.response.use(resp => {

  if (!resp.data.status && resp.data.info === '登陆超时！') {

  }

  return resp.data;
}, error => {
  return Promise.reject(error);
});
export default Axios;
